<?php

/**
 *Simple CRUD Code Generator
 *Email:  support@crazedcoders.com
 *Website: http://www.crazedcoders.com/
 *Copyright (C) 2007 CrazedCoders
 *
 *This program is free software: you can redistribute it and/or modify
 *it under the terms of the GNU General Public License as published by
 *the Free Software Foundation, either version 3 of the License, or
 *(at your option) any later version.
 *
 *THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *SOFTWARE.
 *
 *You should have received a copy of the GNU General Public License
 *along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * generateds a Cairngorm command for a given table
 */
function generateModelForTable( $assoc ) {
	$className = toCamelCase( $assoc['table_name'] );
	$classNameUp = strtoupper( $className );
	$classNameLow = strtolower( $className );
	$tableName = $assoc['table_name'];
	$columns = getTableColumns( $tableName );
	$columnsString = join(', ',array_keys($columns));
	$primaryKey = toCamelCaseMember($assoc['column_name']);
	$secondaryKeys = array();
	foreach( $columns as $key=>$value ) {
		$secondaryKeys[$key] = $columns[$key];
	}
	//$secondaryKeys = StdFunc::array_clone( $columns );
	unset( $secondaryKeys[$primaryKey] );

	$export =
'
package '.con::$package.'.model
{
	/**
	 *Simple CRUD Code Generator
	 *Email:  support@crazedcoders.com
	 *Website: http://www.crazedcoders.com/
	 *Copyright (C) 2007 CrazedCoders
	 *
	 *This program is free software: you can redistribute it and/or modify
	 *it under the terms of the GNU General Public License as published by
	 *the Free Software Foundation, either version 3 of the License, or
	 *(at your option) any later version.
	 *
	 *THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	 *SOFTWARE.
	 *
	 *You should have received a copy of the GNU General Public License
	 *along with this program.  If not, see <http://www.gnu.org/licenses/>.
     */

	import '.con::$package.'.vo.'.$className.';
	import flash.utils.Dictionary;
	import flash.events.IEventDispatcher;
	import mx.utils.ArrayUtil;
	import mx.events.PropertyChangeEvent;
	import flash.events.EventDispatcher;
	import mx.collections.ArrayCollection;
	import mx.collections.ListCollectionView;
	import flash.events.Event;


	public class '.$className.'Model extends EventDispatcher
	{
		private static var _instance:'.$className.'Model;

		public static function getInstance():'.$className.'Model
		{
			if (_instance == null)
			{
				_instance = new '.$className.'Model();
			}
			return _instance;
		}

		public function '.$className.'Model()
		{
			if (_instance != null)
	   		{
				throw new Error("Only one Model instance should be instantiated!");
				return;
	   		}
		}



		private var dictionary:Dictionary;
		private var _count:int = -1;
		private var _array:ArrayCollection;

		public function set count(i:int):void {
			this._count = i;
		}
		public function get count():int {
			return( this._count );
		}


		//[Inspectable(category="General", arrayType="Object")]
		[Bindable(event=\'listChanged\')]
		public function get array():Array {
			/*
			var array:ArrayCollection = new ArrayCollection();
			for each ( var item:Object in this.dictionary ) {
				array.addItem( item );
			}
			ListCollectionView
			return( array.toArray() );
			*/
			if( this._array ) {
				return( this._array.toArray() );
			}

			else {
				return( [] );
			}
		}

		public function add( item:'.$className.' ):void {
			if( item ) {
				if( ! dictionary ) {
					this.dictionary = new Dictionary();
				}
				if( ! this._array ) {
					this._array = new ArrayCollection();
				}
				var '.$classNameLow.':'.$className.' = this.getById( item.'.$primaryKey.' );
				if( '.$classNameLow.' != null ) {
					var index:int = this.dictionary[item.'.$primaryKey.'];
					index--;
					this._array[index] = item;
				}
				else {
					this.dictionary[item.'.$primaryKey.'] = this._array.length + 1;
					this._array.addItem(item);
				}
				this.dispatchEvent( new Event(\'listChanged\') );
				//this.dispatchEvent( PropertyChangeEvent.createUpdateEvent(
				//	this, \'array\', null, this.array ));
			}
		}

		public function addArray( items:Array ):void {
			for ( var i:int = 0; i < items.length; i++ ) {
				var item:'.$className.' = '.$className.'( items[i] );
				if( item ) {
					if( ! dictionary ) {
						this.dictionary = new Dictionary();
					}
					if( ! this._array ) {
						this._array = new ArrayCollection();
					}
					var '.$classNameLow.':'.$className.' = this.getById( item.'.$primaryKey.' );
					if( '.$classNameLow.' != null ) {
						var index:int = this.dictionary[item.'.$primaryKey.'];
						index--;
						this._array[index] = item;
					}
					else {
						this.dictionary[item.'.$primaryKey.'] = this._array.length + 1;
						this._array.addItem(item);
					}
					//this.dispatchEvent( PropertyChangeEvent.createUpdateEvent(
					//	this, \'array\', null, this.array ));
				}
			}
			this.dispatchEvent( new Event(\'listChanged\') );
		}

		public function remove( item:'.$className.' ):void {
			if( item ) {
				if( ! dictionary ) {
					this.dictionary = new Dictionary();
				}
				/* if( this.getById( item.'.$primaryKey.' ) ) {
					return;
				} */
				var index:int = this.dictionary[item.'.$primaryKey.'];
				index--;
				delete this.dictionary[item.'.$primaryKey.'];
				if( index >= 0 ) {
					this._array.removeItemAt( index );
				}
				// reindex since the dictionary values and array indexes are
				// off now
				this.reindex();
				this.dispatchEvent( new Event(\'listChanged\') );
			}
		}

		private function reindex():void {
			for( var i:int = 0; i < this._array.length; i++ ) {
				this.dictionary[this._array[i].'.$primaryKey.'] = i + 1;
			}
		}

		public function getById( id:int ):'.$className.' {
			if( this.dictionary ) {
				var index:int = this.dictionary[id];
				index--;
				if( index >= 0 && index < this._array.length ){
					return( this._array[index] );
				}
			}
			return null;
		}


	}
}

';

	return( $export );

}



?>
